// if(localStorage.userId == '') location.replace('login.html')
// else{
	
// }


// 委托重置密码
$('body').on('click','#affirmReset',function(e){
	e.preventDefault();
	let reg = /^\S{5,12}$/;
	
	if($('#oldPass').val() != localStorage.oldPass) return layer.msg('旧密码不一致')
	
	if(!reg.test($('#newPass').val()) || !reg.test($('#passAgin').val())) return layer.msg('密码长度为5~12位字符')
	
	if($('#newPass').val() == localStorage.oldPass) return layer.msg('新密码和旧密码不能相同')
	
	if($('#newPass').val() != $('#passAgain').val()) return layer.msg('两次密码输入不一样')
	
	// console.log(1)
	$.ajax({
		type: 'post',
		url: 'http://misssj.cn:3001/resetPass',
		data: {
			userId: localStorage.userId,
			newPass: $('#newPass').val()
		},
		success: function(data){
			if(data) localStorage.oldPass = $('#newPass').val()
			layer.open({
			  title: '处理结果',
			  content: data
			}); 
		}
	})
})

// 委托上传头像
$('body').on('click','#uploadPic',function(){
	$('#upload_pic').click()
})

$('body').on('change','#upload_pic',function(){
	if($(this).val() == '') return
	let url = URL.createObjectURL(this.files[0])
	$('#pic').cropper('destroy').attr('src',url).cropper({
		aspectRatio:1,
		preview: '#preview'
	})
	setTimeout( () => {
		$('span.cropper-point,span.cropper-line').remove()
	},100)
})


// 公共header
let ms = new Vue({
	el:'#ms',
	data:{
		
	},
	methods:{
		// 退出登录
		logout: function(){
			layer.confirm('退出后将重新登录', {icon: 0, title:'确定退出?'}, function(index){
			  localStorage.removeItem( 'userId' );
			  location.replace('login.html')
			});
		},
		// 重置密码
		reset: function(e){
			setTimeout( () => {
				$(e.target).parent('dd').removeClass('layui-this')
			},0)
			layer.open({
				type:1,
				title: '重置密码',
				content: $('#resetPass').html(),
				area: ['500px', '300px']
			});
		},
		// 订单管理
		order_list: function(e){
			if(e.target.nodeName != 'A') return
			localStorage.state = $(e.target).data('state')
		}
	}
})

// 个人资料页
let datum = new Vue({
	el:'#datum',
	data:{
		datumObj:'',
		sex: undefined,
		like: ''
	},
	methods:{
		// 弹出修改头像
		show_modal: function(){
			layer.open({
				type:1,
				title: '重置密码',
				content: $('#modal').html(),
				area: ['600px', '400px']
			});
			$('#pic').cropper({
				aspectRatio:1,
				preview: '#preview'
			})
			
		},
		// 点击修改信息
		modify_datum: function(e){
			datum_out(e,1)
			initialize_datum(datum.datumObj,1)
		},
		// 点击确认修改
		submit_datum: function(e){
			$.ajax({
				type: 'post',
				url: 'http://misssj.cn:3001/modifyDatum',
				data: {
					userId: localStorage.userId,
					obj: JSON.stringify(this.datumObj)
				},
				success: function(data){
					layer.msg('修改成功', {icon: 6})
					initialize_datum(data,0)
					datum_out(e,0)
				}
			})
		},
	}
})

let order = new Vue({
	el:'#order',
	data:{
		title: localStorage.state,
		orderList: ''
	},
	methods:{
		check_order: function(){
			layer.open({
				type: 1,
				title: '订单详情',
				content:'订单表格',
				area: ['600px', '400px']
			})
		},
		delete_order: function(id){
			// console.log(id)
			$.ajax({
				type: 'post',
				url: 'http://misssj.cn:3001/deleteOrder',
				data: {
					userId: localStorage.userId,
					orderId: id
				},
				success: function(data){
					console.log(data)
					order.orderList = data.order
				}
			})
		}
	}
})


get_datum()
get_order()



// 修改表单状态
function datum_out(e,staut){
	$(e.target).hide().siblings('button').show()
	if(staut){
		$('.layui-card span').nextAll().show();
		$('.layui-card input').removeAttr('disabled')
	}else{
		$('.layui-card span').nextAll().hide();
		$('.layui-card input').prop('disabled',true)
	}
}

// 初始化选择控件
function initialize_datum(data,staut){
	datum.datumObj = data
	datum.sex = staut ? undefined : JSON.parse(data.sex)
	datum.like = staut ? '' : data.like.join('、')
}

// 拿用户数据函数
function get_datum(){
	$.ajax({
		url: 'http://misssj.cn:3001/datum',
		data: {
			userId: localStorage.userId
		},
		success: function(data){
			initialize_datum(data,0)
		}
	})
}

// 拿订单数据
function get_order(){
	$.ajax({
		url: 'http://misssj.cn:3001/order',
		data: {
			state: localStorage.state,
			userId: localStorage.userId
		},
		success: function(data){
			console.log(data)
			order.orderList = data
		}
	})
}
